﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ADB_QuanLyThuVien
{
    public partial class frmReturnBook : DevExpress.XtraEditors.XtraForm
    {
        public frmReturnBook()
        {
            InitializeComponent();
        }

        public frmReturnBook(Account acc)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            this.acc = acc;
        }
        Account acc = new Account();
        List<Book> lReturnBook = new List<Book>();
        private void frmReturnBook_Load(object sender, EventArgs e)
        {
            List<Member> lMember = new List<Member>();
            lMember = DAO_PhieuMuon.SelectMemberNotRB();
            foreach (var item in lMember)
            {
                cbbDocGia.Properties.Items.Add(item.Email);
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            List<PhieuMuon> lPhieuMuon = DAO_PhieuMuon.SelectPhieuMuon(cbbDocGia.SelectedText);
            phieuMuonBindingSource.DataSource = lPhieuMuon;
        }

        private void cbbDocGia_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<PhieuMuon> lPhieuMuon = DAO_PhieuMuon.SelectPhieuMuon(cbbDocGia.SelectedText);
            phieuMuonBindingSource.DataSource = lPhieuMuon;
        }

        private void phieuMuonComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Book> lBookBorrow = DAO_PhieuMuon.SelectBorrowBook(phieuMuonComboBox.SelectedValue.ToString());
            while (list_SachDaMuon.ItemCount != 0)
            {
                list_SachDaMuon.SelectedItem = 0;
                //list_SachTra.Items.Add(list_SachDaMuon.SelectedValue);
                list_SachDaMuon.Items.RemoveAt(list_SachDaMuon.SelectedIndex);
            }
            while (list_SachTra.ItemCount != 0)
            {
                list_SachTra.SelectedItem = 0;
                //list_SachDaMuon.Items.Add(list_SachTra.SelectedValue);
                list_SachTra.Items.RemoveAt(list_SachTra.SelectedIndex);
            }
            for (int i = 0; i < lBookBorrow.Count; ++i)
                list_SachDaMuon.Items.Add(lBookBorrow[i].TenSach);
        }

        private void btn_next_Click(object sender, EventArgs e)
        {
            if (list_SachDaMuon.ItemCount != 0)
            {
                list_SachTra.Items.Add(list_SachDaMuon.SelectedValue);
                string tmp = list_SachDaMuon.SelectedValue.ToString();
                ThemBook(tmp, phieuMuonComboBox.SelectedText);
                list_SachDaMuon.Items.RemoveAt(list_SachDaMuon.SelectedIndex);
            }
        }

        private void ThemBook(string TenSach, string maphieu)
        {
            Book book = DAO_PhieuMuon.SelectBorrowBookByName(TenSach,maphieu);
            lReturnBook.Add(book);
        }

        private void btn_pre_Click(object sender, EventArgs e)
        {
            if (list_SachTra.ItemCount != 0)
            {
                list_SachDaMuon.Items.Add(list_SachTra.SelectedValue);
                string tmp = list_SachTra.SelectedValue.ToString();
                XoaBook(tmp, lReturnBook);
                list_SachTra.Items.RemoveAt(list_SachTra.SelectedIndex);
            }
        }

        private void XoaBook(string tensach, List<Book> lReturnBook)
        {
            for (int i = 0; i < lReturnBook.Count; ++i)
                if (lReturnBook[i].TenSach == tensach)
                {
                    lReturnBook.RemoveAt(i);
                    break;
                }
        }
     
        private void btn_nextall_Click(object sender, EventArgs e)
        {
            while (list_SachDaMuon.ItemCount != 0)
            {
                list_SachDaMuon.SelectedItem = 0;
                list_SachTra.Items.Add(list_SachDaMuon.SelectedValue);
                string tmp = list_SachDaMuon.SelectedValue.ToString();
                ThemBook(tmp, phieuMuonComboBox.SelectedText);
                list_SachDaMuon.Items.RemoveAt(list_SachDaMuon.SelectedIndex);
            }
        }

        private void btn_preall_Click(object sender, EventArgs e)
        {
            while (list_SachTra.ItemCount != 0)
            {
                list_SachTra.SelectedItem = 0;
                list_SachDaMuon.Items.Add(list_SachTra.SelectedValue);
                string tmp = list_SachTra.SelectedValue.ToString();
                XoaBook(tmp, lReturnBook);
                list_SachTra.Items.RemoveAt(list_SachTra.SelectedIndex);
            }
        }

        private void btn_DongY_Click(object sender, EventArgs e)
        {
            PhieuTra pt = new PhieuTra();
            MessageBox.Show(lReturnBook.Count.ToString());
            string kq = DAO_ReturnBook.CreateReturnForm(pt, acc, phieuMuonComboBox.SelectedValue.ToString(), lReturnBook);
            MessageBox.Show(kq);
        }
    }
}